package gov.cdc.epiinfo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Environment;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceFragment;
import android.preference.SwitchPreference;

public class SettingsFragment extends PreferenceFragment {

	private Activity activity;

	public void SetActivity(Activity activity)
	{
		this.activity = activity;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Load the preferences from an XML resource
		addPreferencesFromResource(R.xml.preferences);

		final CheckBoxPreference ei7 = (CheckBoxPreference) getPreferenceManager().findPreference("ei7");
		final CheckBoxPreference stacked = (CheckBoxPreference) getPreferenceManager().findPreference("stacked");
		final CheckBoxPreference sync_up_only = (CheckBoxPreference) getPreferenceManager().findPreference("sync_up_only");
		final CheckBoxPreference sync_up_down = (CheckBoxPreference) getPreferenceManager().findPreference("sync_up_down");
		final SwitchPreference sample_forms = (SwitchPreference) getPreferenceManager().findPreference("sample_forms");

		if (DeviceManager.IsPhone())
		{
			ei7.setEnabled(false);
			stacked.setChecked(true);
		}

		ei7.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			@Override
			public boolean onPreferenceClick(Preference arg0) {
				ei7.setChecked(true);
				stacked.setChecked(false);
				return true;
			}
		});

		stacked.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			@Override
			public boolean onPreferenceClick(Preference arg0) {
				stacked.setChecked(true);
				ei7.setChecked(false);
				return true;
			}
		});

		sync_up_only.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			@Override
			public boolean onPreferenceClick(Preference arg0) {
				sync_up_only.setChecked(true);
				sync_up_down.setChecked(false);
				return true;
			}
		});

		sync_up_down.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			@Override
			public boolean onPreferenceClick(Preference arg0) {
				sync_up_only.setChecked(false);
				sync_up_down.setChecked(true);
				return true;
			}
		});

		sample_forms.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			@Override
			public boolean onPreferenceChange(Preference arg0, Object val) {
				if ((Boolean)val)
				{
					GetSampleForm();
				}
				else
				{
					DeleteSampleForm();
				}
				return true;
			}
		});
	}

	private void DeleteSampleForm()
	{
		try
		{
			File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+"/EpiInfo/Questionnaires/Sample_Preparedness.xml");
			file.delete();
		}
		catch (Exception ex)
		{

		}
	}

	private void GetSampleForm()
	{
		AssetManager am = activity.getAssets();
		try 
		{          
			String fileName = "Sample_Preparedness.xml";
			File destinationFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+"/EpiInfo/Questionnaires/" + fileName);
			InputStream in = am.open(fileName);         
			FileOutputStream f = new FileOutputStream(destinationFile);          
			byte[] buffer = new byte[1024];         
			int len1 = 0;         
			while ((len1 = in.read(buffer)) > 0) 
			{             
				f.write(buffer, 0, len1);         
			}         
			f.close();     
		} 
		catch (Exception e) 
		{         

		}
	}
}